package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import edu.gxnu.cetcdemo.mapper.ClaimMapper;
import edu.gxnu.cetcdemo.mapper.PropMapper;
import edu.gxnu.cetcdemo.mapper.UserMapper;
import edu.gxnu.cetcdemo.pojo.Claim;
import edu.gxnu.cetcdemo.pojo.ClaimDTO;
import edu.gxnu.cetcdemo.pojo.Prop;
import edu.gxnu.cetcdemo.pojo.User;
import edu.gxnu.cetcdemo.service.ClaimService;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【claim】的数据库操作Service实现
* @createDate 2024-04-02 15:09:11
*/
@Service
public class ClaimServiceImpl extends ServiceImpl<ClaimMapper, Claim>
    implements ClaimService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PropMapper propMapper;
    @Autowired
    private ClaimMapper claimMapper;

    private static final int PAGESIZE = 9;

    @Override
    public Result saveResult(Map<String, Object> map){
        Integer userId = null;

        String studentId = (String) map.get("studentId");
        Integer propId = (Integer) map.get("propId");

//        System.out.println("claimtest" + propId + " " + studentId);

        LambdaQueryWrapper<User> userLQW = new LambdaQueryWrapper<>();
        userLQW.eq(User::getStudentId, studentId).select(User::getUserId);
        User user = userMapper.selectOne(userLQW);
        if (user==null){
            return new Result<>().build(400,"输入学号不存在！");
        }
        userId = user.getUserId();
//        System.out.println("claimtest" + user);
//        System.out.println("claimtest"  + userId + " ");
        //更新失物状态
        LambdaUpdateWrapper<Prop> propLUW = new LambdaUpdateWrapper<>();
        propLUW.eq(Prop::getPropId,propId).set(Prop::getPropState,"已申领");
        propMapper.update(null,propLUW);

        Claim claim = new Claim();
        claim.setUserId(userId);
        claim.setPropId(propId);
        claim.setClaimTime(new Timestamp(System.currentTimeMillis()));


        try {
            if (this.save(claim)) {
                return new Result().build(200, "申领记录保存成功！", null);
            }
            return new Result().build(500, "申领记录保存失败！", null);
        } catch (Exception e) {
            if (e.getCause().getMessage().startsWith("Duplicate")) {
                return new Result().build(400, "该条记录已存在！", null);
            }

            if (e.getCause().getMessage().startsWith("Cannot add or update a child row: a foreign key constraint fails")) {
                return new Result().build(400, "学号错误或失物不存在！", null);
            }
            return new Result<>().build(400,e.getMessage(),null);
        }
    }

    @Override
    public Result get(Integer pageNumber){
        MPJLambdaWrapper<Claim> claimLQW = new MPJLambdaWrapper<Claim>();
        claimLQW.selectAll(Claim.class)
                .select(User::getUserName)
                .select(Prop::getPropName)
                .innerJoin(User.class,User::getUserId,Claim::getUserId)
                .innerJoin(Prop.class,Prop::getPropId,Claim::getPropId);

        Page claimPage = (Page) claimMapper.selectJoinPage(new Page<>(pageNumber,PAGESIZE), ClaimDTO.class, claimLQW);
        List<ClaimDTO> list = claimPage.getRecords();




        return new Result().build(200,"查询成功",list);
    }
}




